package com.itwn.controller;

import com.github.pagehelper.PageInfo;
import com.itwn.entity.Schedule;
import com.itwn.result.ResponseResult;
import com.itwn.service.CinemaService;
import com.itwn.service.MovieService;
import com.itwn.service.ScheduleService;
import com.itwn.vo.ScheduleQueryVo;
import com.itwn.vo.ScheduleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("schedule")
@CrossOrigin
@Api(value = "电影排片")
public class ScheduleController {
    @Autowired
    ScheduleService scheduleService;
    @Autowired
    CinemaService cinemaService;
    @Autowired
    MovieService movieService;

    @PostMapping("/add")
    @ApiOperation(value = "添加排片计划")
    public ResponseResult<Schedule> createSchedule(@RequestBody ScheduleVo scheduleVo){
        Schedule schedule = new Schedule();
        schedule.setCinemaHallId(cinemaService.findCinemaHallId(scheduleVo.getCinemaId(),scheduleVo.getHtId()));
        schedule.setMovieId(scheduleVo.getMovieId());
        schedule.setSchedulePrice(scheduleVo.getPrice());
        schedule.setScheduleStartTime(scheduleVo.getStartTime());
        schedule.setScheduleEndTime(scheduleVo.getEndTime());
        schedule.setScheduleState("1");
        ResponseResult<Schedule> result = new ResponseResult<>();
        int i = scheduleService.addSchedule(schedule);
        if(i>0){
            result.setCode(200);
            result.setMsg("添加成功");
            result.setData(schedule);
            return result;
        }else{
            result.setCode(201);
            result.setMsg("添加失败");
            return result;
        }
    }
    @GetMapping("/pageSearch")
    @ApiOperation(value = "分页展示排片列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页的条数",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name="movieName",value = "电影名称",required = false,dataType = "string",paramType = "query"),
            @ApiImplicitParam(name="cinemaId",value = "影院ID",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name="htId",value = "影厅类型ID",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name="startTime",value = "开始时间",required = false,dataType = "string",paramType = "query"),
            @ApiImplicitParam(name="endTime",value = "结束时间",required = false,dataType = "string",paramType = "query")
    })
    public ResponseResult<PageInfo<ScheduleQueryVo>> pageSearch(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                @RequestParam(defaultValue = "8") Integer pageSize,
                                                                @RequestParam(value = "movieName") String movieName,
                                                                @RequestParam(defaultValue = "0") Integer cinemaId,
                                                                @RequestParam(defaultValue = "0") Integer htId,
                                                                String startTime, String endTime) throws ParseException {
        ResponseResult<PageInfo<ScheduleQueryVo>> result = new ResponseResult<>();
        if("null".equals(movieName)){
            movieName=null;
        }
        int cinemaHallId=0;
        if(cinemaId!=0&&htId!=0){
            cinemaHallId = cinemaService.findCinemaHallId(cinemaId, htId);
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date scheduleStartTime=null;
        if(startTime!=null&&!"".equals(startTime)){
            scheduleStartTime= sdf.parse(startTime);
        }
        Date scheduleEndTime = null;
        if(endTime!=null&&!"".equals(endTime)){
            scheduleEndTime=sdf.parse(endTime);
        }
        PageInfo<ScheduleQueryVo> pageInfo=scheduleService.pageSearch(pageNum,pageSize,cinemaHallId,movieName,scheduleStartTime,scheduleEndTime);
        if(pageInfo.getList().size()>0){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        }else{
            result.setCode(205);
            result.setMsg("查询失败");
        }
        return result;
    }
    @DeleteMapping("/remove")
    @ApiOperation(value = "删除排片计划")
    @ApiImplicitParam(name="scheduleId",value = "排片ID",required = true,dataType = "Integer",paramType = "query")
    public ResponseResult<Schedule> remove(Integer scheduleId){
        int i=scheduleService.remove(scheduleId);
        ResponseResult<Schedule> result = new ResponseResult<>();
        if(i>0){
            result.setCode(200);
            result.setMsg("删除成功");
        }else{
            result.setCode(206);
            result.setMsg("删除失败");
        }
        return result;
    }
    @PutMapping("/modify")
    @ApiOperation(value = "更新排片计划")
    public ResponseResult<Schedule> modify(@RequestBody ScheduleVo scheduleVo){
        ResponseResult<Schedule> result = new ResponseResult<>();
        Schedule schedule = new Schedule();
        schedule.setCinemaHallId(cinemaService.findCinemaHallId(scheduleVo.getCinemaId(),scheduleVo.getHtId()));
        schedule.setMovieId(scheduleVo.getMovieId());
        schedule.setSchedulePrice(scheduleVo.getPrice());
        schedule.setScheduleStartTime(scheduleVo.getStartTime());
        schedule.setScheduleEndTime(scheduleVo.getEndTime());
        schedule.setScheduleId(scheduleVo.getScheduleId());
        System.out.println(schedule);
        int i=scheduleService.modify(schedule);
        if(i>0){
            result.setCode(200);
            result.setMsg("更新成功");
            result.setData(schedule);
        }else{
            result.setCode(208);
            result.setMsg("更新失败");
        }
        return result;
    }
    @GetMapping("/getMovieTime")
    @ApiOperation("添加排片计划，根据开始时间生成结束时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name="movieId",value = "电影ID",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name="cinemaId",value = "影院ID",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name="htId",value = "影厅类型ID",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name="time",value="开始时间",required = true,dataType = "string",paramType = "query")
    })
    public ResponseResult<Date> getMovieTime(@RequestParam(value = "movieId") Integer movieId,
                                             @RequestParam(defaultValue = "0" ,value = "cinemaId") Integer cinemaId,
                                             @RequestParam(value = "htId",defaultValue = "0") Integer htId,
                                             String time) throws ParseException {
        Integer cinemaHallId=0;
        if(cinemaId!=0&&htId!=0){
            cinemaHallId = cinemaService.findCinemaHallId(cinemaId, htId);
        }
        String movieTime=movieService.findMovieTime(movieId);
        String[] s = movieTime.split("分钟");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = sdf.parse(time);
        Date endTime=new Date(startTime.getTime()+Integer.valueOf(s[0])*60*1000+30*60*1000);
        ResponseResult<Date> result = new ResponseResult<>();
        if(cinemaHallId==0){
            result.setCode(211);
            result.setMsg("暂无此影厅安排");
        }else{
            int i=scheduleService.findRepeatedSchedule(cinemaHallId,movieId,startTime,endTime);
            if(i==0){
                result.setCode(200);
                result.setMsg("无重复排片计划");
                result.setData(endTime);
            }else{
                result.setCode(209);
                result.setMsg("有重复排片计划");
            }
        }
        return result;
    }
    @GetMapping("/modifyTime")
    @ApiOperation(value = "更新排片,更改开始时间生成结束时间")
    @ApiImplicitParams({
            @ApiImplicitParam(name="movieId",value = "电影ID",required = true,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name="cinemaId",value = "影院ID",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name="htId",value = "影厅类型ID",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name="time",value="开始时间",required = true,dataType = "string",paramType = "query")
    })
    public ResponseResult<Date> getUpdateTime(@RequestParam Integer cinemaId, Integer htId, String time, Integer movieId) throws ParseException {
        ResponseResult<Date> result = new ResponseResult<>();
        Integer cinemaHallId=0;
        if(cinemaId!=0&&htId!=0){
            cinemaHallId = cinemaService.findCinemaHallId(cinemaId, htId);
        }
        String movieTime = movieService.findMovieTime(movieId);
        String[] s = movieTime.split("分钟");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = sdf.parse(time);
        Date endTime=new Date(startTime.getTime()+Integer.valueOf(s[0])*60*1000+30*60*1000);
        int i=scheduleService.findRepeatedScheduleForUpdate(cinemaHallId,startTime,endTime);
        if(i==0){
            result.setCode(200);
            result.setMsg("无重复排片计划");
            result.setData(endTime);
        }else{
            result.setCode(210);
            result.setMsg("有重复排片计划");
        }
        return result;
    }
}